﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
using Model;

namespace POS.ModForm.Sale
{
    public partial class AddItemSaleUC : DevExpress.XtraEditors.XtraUserControl
    {
        public AddItemSaleUC()
        {
            InitializeComponent();
        }
        public ComboBoxEdit StoreWarehouse
        {
            get { return this._StoreWarehouse; }
        }
        public LookUpEdit LookUpProduct
        {
            get { return this.lookUpEditItem; }
        }
        public TextEdit SellPriceControl
        {
            get { return this._SellPrice; }
        }
        public SimpleButton AddDetail
        {
            get { return this.btnAdd; }
        }

        public SimpleButton DelDetail
        {
            get { return this.btnDelete; }
        }
        public string WarehouseID
        {
            get
            {
                return dicWarehouse[this._StoreWarehouse.Text];
            }
        }
        public string ProductID
        {
            get
            {
                if (string.IsNullOrEmpty(lookUpEditItem.Text.Trim()) || lookUpEditItem.Text.Trim() == "输入货品简码")
                {
                    lookUpEditItem.Select();
                    throw new Exception("请选择要销售的货品！");
                }
                DataRowView item = (DataRowView)lookUpEditItem.GetSelectedDataRow();
                return item.Row["ProductID"].ToString();
            }
        }
        public decimal Quantity
        {
            get
            {
                if (string.IsNullOrEmpty(this._Quantity.Text.Trim()) || Convert.ToDecimal(this._Quantity.Text.Trim()) <= 0)
                {
                    this._Quantity.Select();
                    throw new Exception("请输入购买货品数量！");
                }
                return Convert.ToDecimal(this._Quantity.Text.Trim());
            }
        }
        public decimal SellPrice
        {
            get
            {
                if (string.IsNullOrEmpty(this._SellPrice.Text.Trim()) || Convert.ToDecimal(this._SellPrice.Text.Trim()) <= 0)
                {
                    _SellPrice.Select();
                    throw new Exception("请输入货品的销售单价！");
                }
                else
                {
                    DataRowView item = (DataRowView)lookUpEditItem.GetSelectedDataRow();
                    if (Convert.ToDecimal(this._SellPrice.Text.Trim()) < Convert.ToDecimal(item.Row["SalePriceLowest"]))
                        throw new Exception("销售价小于了最低销售价！");
                }
                return Convert.ToDecimal(this._SellPrice.Text.Trim());
            }
        }
        public DataRowView ProductPrice
        {
            get
            {
                DataRowView item = (DataRowView)lookUpEditItem.GetSelectedDataRow();
                if (item == null)
                    throw new Exception("未选择要销售的商品！");
                return item;
            }
        }
        WarehouseBLL warehouseBLL = new WarehouseBLL();
        Dictionary<string, string> dicWarehouse = new Dictionary<string, string>();
        public void InitBindWarehouse()
        {
            foreach (B_Warehouse w in warehouseBLL.GetWarehouses(MemberInfo.Store.StoreID))
            {
                this._StoreWarehouse.Properties.Items.Add(w.WarehouseName);
                dicWarehouse.Add(w.WarehouseName, w.WarehouseID);
            }
            if (MemberInfo.UserPosSetUp.StoreSaleWarehouse != null)
                this._StoreWarehouse.SelectedText = MemberInfo.UserPosSetUp.StoreSaleWarehouse.WarehouseName;
        }
        public void ControlResetText()
        {
            this.lookUpEditItem.ResetText();
            this._Quantity.ResetText();
            this._Quantity.Text = string.Empty;
            this._Quantity.ToolTip = string.Empty;
            this._SellPrice.ResetText();
            this._SellPrice.Text = string.Empty;
            this._SellPrice.ToolTip = string.Empty;
            this.lookUpEditItem.Select();
        }
        private void AddItemSaleUC_Load(object sender, EventArgs e)
        {
            this._StoreWarehouse.Properties.Items.Clear();
            this.lookUpEditItem.Properties.ShowHeader = true;
            this.lookUpEditItem.Properties.PopupWidth = 600;
            this.lookUpEditItem.Properties.PopupWidth = 300;
        }
        private void _Quantity_Click(object sender, EventArgs e)
        {
            this._Quantity.SelectAll();
        }
        private void _SellPrice_Click(object sender, EventArgs e)
        {
            this._SellPrice.SelectAll();
        }

        private void lookUpEditItem_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.lookUpEditItem.Text.Trim()))
            {
                return;
            }
            DataRowView item = this.ProductPrice;
            this._Quantity.SelectAll();
            this._Quantity.ToolTip = StockDetailBLL.GetStockDetail(this.WarehouseID, this.ProductID, DatabaseSchema.Session).Quantity.ToString();
            this._SellPrice.Text = Convert.ToDecimal(item.Row["SalePrice"]).ToString("f2");
            this._SellPrice.ToolTip = Convert.ToDecimal(item.Row["SalePriceLowest"]).ToString("f2");
        }

        private void lookUpEditItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                lookUpEditItem_Leave(sender, e);
        }
    }
}
